﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

namespace VisionRoc.Tool
{
    public class TxtAPI
    {
        //private static FileStream fs_A;
        //private static StreamWriter sw_A;

        /// <summary>
        /// 写Debug路径下LOG文本
        /// </summary>
        /// <param name="Message"></param>
        public static async void WriteLogTxt(string Message)
        {
            await Task.Run(() =>
            {
                try
                {
                    string path = Application.StartupPath + "\\" + "Log" + "\\" + Tool.Time();
                    if (Directory.Exists(path) == false)//判断文本路径是否存在 不存在则创建路径
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + "\\" + DateTime.Now.ToString("yyyyMMddHH") + ".Log";
                    using(FileStream fs_A = new FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write))
                    {
                        using (StreamWriter sw_A = new StreamWriter(fs_A, System.Text.Encoding.Default))
                        {
                            string testdata = "";
                            testdata = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "==>" + Message + "\r\n";
                            sw_A.WriteLine(testdata);
                            sw_A.Flush();
                            sw_A.Close();
                            sw_A.Dispose();
                            fs_A.Flush();
                            fs_A.Close();
                            fs_A.Dispose();
                        }
                    }
                }
                catch { }
            });
        }

        /// <summary>
        /// 写入LOG文本
        /// </summary>
        /// <param name="Message"></param>
        public static async void WriteLogTxt(string m_path, string Message)
        {
            await Task.Run(() =>
            {
                try
                {
                    string path = m_path + "\\" + "Log" + "\\" + Tool.Time();
                    if (Directory.Exists(path) == false)//判断文本路径是否存在 不存在则创建路径
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + "\\" + DateTime.Now.ToString("yyyyMMddHH") + ".Log";

                    using (FileStream fs_A2 = new FileStream(path, System.IO.FileMode.Append, System.IO.FileAccess.Write))
                    {
                        using (StreamWriter sw_A2 = new StreamWriter(fs_A2, System.Text.Encoding.Default))
                        {
                            string testdata = "";
                            testdata = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "==>" + Message + "\r\n";
                            sw_A2.WriteLine(testdata);
                            sw_A2.Flush();
                            sw_A2.Close();
                            sw_A2.Dispose();
                            fs_A2.Flush();
                            fs_A2.Close();
                            fs_A2.Dispose();
                        }
                    }
                }
                catch { }
            });
        }
    }
}
